/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Model.Dao.DAOLoaiNhanVien;
import Model.Dao.DAONhanVien;
import Model.Pojo.Loainhanvien;
import Model.Pojo.Nhanvien;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author THANH QUAN
 */
public class DeleteUpdateNhanVien
        extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException, Exception
    {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setAttribute("QUAN_LY_NHAN_VIEN", 1);

        response.setContentType("text/html;charset=UTF-8");
        if (request.getServletPath().equals("/delete-nhan-vien.htm"))
        {
            String queryString = request.getQueryString();
            if (queryString != null)
            {
                String[] arr = queryString.split("=");
                DAONhanVien DAOnv = new DAONhanVien();
                int id = Integer.parseInt(arr[1]);
                DAOnv.DeleteNhanVien(id);
            }
            response.sendRedirect("danh-sach-nhan-vien.htm");
        }
        else
        {
            String queryString = request.getQueryString();
            if (queryString != null)
            {
                String[] arr = queryString.split("=");
                //Get thông tin nhân viên
                DAONhanVien DAOnv = new DAONhanVien();
                Nhanvien nv = DAOnv.GetNhanVien(Integer.parseInt(arr[1]));
                if (nv != null)
                {
                    HttpSession session = request.getSession(true);
                    session.setAttribute("Chitietnhanvien", nv);
                    request.setAttribute("Chitietnhanvien", nv);
                    DAOLoaiNhanVien DAOloainv = new DAOLoaiNhanVien();
                    List<Loainhanvien> dsLoaiNV = DAOloainv.DanhSachLoaiNhanVien();
                    request.setAttribute("dsLoaiNV", dsLoaiNV);
                }
                else
                {
                    request.setAttribute("Message", "Thông tìm thấy thông tin nhân viên");
                }
                RequestDispatcher view = request.getRequestDispatcher("them-nhan-vien.jsp");
                view.forward(request, response);
            }
            else
            {
                String urlRedirect = "danh-sach-nhan-vien.htm";
                String btnCapNhat = request.getParameter("btnCapNhatNhanVien");
                if (btnCapNhat != null)
                {
                    HttpSession session = request.getSession(true);
                    Nhanvien nv = (Nhanvien) session.getAttribute("Chitietnhanvien");
                    int id = Integer.parseInt(request.getParameter("ddlLoaiNV"));
                    nv.setLoainhanvien(new Loainhanvien(id));
                    nv.setManv(request.getParameter("txtMaNV"));
                    nv.setMatkhau(request.getParameter("txtMatKhau"));
                    nv.setHoten(request.getParameter("txtHoTen"));
                    nv.setGioitinh(request.getParameter("chbGioiTinh"));
                    nv.setDiachi(request.getParameter("txtDiaChi"));
                    nv.setSodienthoai(request.getParameter("txtDienThoai"));
                    nv.setEmail(request.getParameter("txtEmail"));
                    nv.setCmnd(request.getParameter("txtCMND"));
                    nv.setNgaysinh(CXuLy.ConvertStringToDate(request.getParameter("txtNgaySinh")));
                    nv.setGhichu(request.getParameter("txtGhiChu"));
                    DAONhanVien DAOnv = new DAONhanVien();
                    try
                    {
                        DAOnv.CapNhatNhanVien(nv);
                        response.sendRedirect(urlRedirect);
                    }
                    catch (Exception ex)
                    {
                        request.setAttribute("Message", ex.getMessage());
                        urlRedirect = "them-nhan-vien.htm";
                    }
                }
                RequestDispatcher view = request.getRequestDispatcher(urlRedirect);
                view.forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
             if(!CXuLy.PhanQuyen(request, response))
        {
            return;
        }
        try
        {
            processRequest(request, response);
        }
        catch (ParseException ex)
        {
            Logger.getLogger(DeleteUpdateNhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception ex)
        {
            Logger.getLogger(DeleteUpdateNhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            processRequest(request, response);
        }
        catch (ParseException ex)
        {
            Logger.getLogger(DeleteUpdateNhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception ex)
        {
            Logger.getLogger(DeleteUpdateNhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
